/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.pearreview.util.pdf;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.pearreview.application.Messages;

public class PdfPageEventOld extends PdfPageEventHelper {
	

	private PdfTemplate template = null;
	private PdfFontManager fonts = PdfFontManager.getInstance();
	private final int FONT_SIZE = 12;
	private String headerText = "";
	
	public PdfPageEventOld(String headerText) {
		this.headerText = headerText;
	}
	
	private void writePunchMarks(PdfContentByte cb, Document document) {
		float height = ptToCm(document.getPageSize().getHeight());
		
		cb.setColorStroke(new BaseColor(0x333333));

		cb.setLineWidth(0.0f);

		cb.moveTo(0.0f, cmToPt(height / 2.0f));
		cb.lineTo(cmToPt(0.6f), cmToPt(height / 2.0f));
		
		cb.moveTo(0.0f, cmToPt(height * 0.33f));
		cb.lineTo(cmToPt(0.6f), cmToPt(height * 0.33f));

		cb.moveTo(0.0f, cmToPt(height * 0.66f));
		cb.lineTo(cmToPt(0.6f), cmToPt(height * 0.66f));

		cb.stroke();
		cb.setColorStroke(new BaseColor(0x000000));
	}

	@Override
	public void onOpenDocument(PdfWriter writer, Document document) {
		template = writer.getDirectContent().createTemplate(100, 100);
	}

	@Override
	public void onCloseDocument(PdfWriter writer, Document document) {
		template.beginText();
		template.setFontAndSize(fonts.getBaseFont(), FONT_SIZE);
		template.setTextMatrix(0, 0);
		template.showText(Integer.toString(writer.getPageNumber() - 1));
		template.endText();
	}

	@Override
	public void onEndPage(PdfWriter writer, Document document) {

		try {
			PdfContentByte cb = writer.getDirectContent();	
			
			//draw head
			cb.setLineWidth(1.0f);
			cb.moveTo(document.left(), document.top()+10);
			cb.lineTo(document.right(), document.top()+10);
			cb.stroke();
			
			cb.beginText();
			cb.setFontAndSize(fonts.getBaseFont(), FONT_SIZE);
			cb.setTextMatrix(document.left(), document.top() + 15);
			cb.showText(headerText);
			cb.endText();
			
			writePunchMarks(cb, document);
			

			//page number
			
			cb.saveState();
			
			//Page x of
			String text = 
					Messages.getString("PearReview.invitation.page") + " "
					+ writer.getPageNumber() + "  " + 
					Messages.getString("PearReview.Reviewer.report.of") + 
					" ";
			float textSize = fonts.getBaseFont().getWidthPoint(text, 
					FONT_SIZE);


			cb.setLineWidth(1.0f);
			cb.moveTo(document.left(), document.bottom());
			cb.lineTo(document.right(), document.bottom());
			cb.stroke();
			
			cb.beginText();
			cb.setFontAndSize(fonts.getBaseFont(), FONT_SIZE);
			cb.setTextMatrix(document.left(), 25);
			cb.showText(text);
			cb.endText();
			cb.addTemplate(template, document.left() + textSize, 25);
			
			cb.restoreState();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private float cmToPt(float cm) {
		return ((cm / 2.54f) * 72.0f);
	}
	
	private  float ptToCm(float pt) {
		return ((pt / 72.0f) * 2.54f);
	}
}
